/**
 * Copyright (c) Huawei Technologies Co., Ltd. 1987-2021. All rights reserved.
*/

package com.gitee.fufu669.aspect;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.config.exception.CacheServerErrorCode;
import com.gitee.fufu669.config.exception.CacheServerException;
import com.gitee.fufu669.service.CacheService;
import com.gitee.fufu669.utils.CacheAopUtil;
import com.gitee.fufu669.utils.CacheNetworkUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @author wangfupeng on 2017-5-6.
 */
@Aspect
// 组件注解
@Component
// 注解
@Order(-67)
// 注解
@SuppressWarnings({"rawtypes"})
/**
 * TODO
*/
public class CheckOrderByAop {

    /**
     * TODO
    */
    public static final Logger logger = LoggerFactory.getLogger(CheckOrderByAop.class);

    // 注解
    @Autowired
    // TODO
    private HttpServletRequest request;

    // 注解
    @Before("@annotation(com.gitee.fufu669.aspect.CheckOrderBy)")
    /**
     * TODO
    */
    public void beforeMethod(JoinPoint joinPoint) {
        // 赋值
        String description = "";
        // 赋值
        String methodName = "";
        // 尝试下面的代码
        try {
            // 字符串赋值
            String targetName = joinPoint.getTarget().getClass().getName();
            // TODO
            methodName = joinPoint.getSignature().getName();
            // TODO
            Object[] arguments = joinPoint.getArgs();
            // TODO
            Class targetClass = Class.forName(targetName);
            // TODO
            Method[] methods = targetClass.getMethods();
            // 遍历
            for (Method method : methods) {
                // 判断
                if (method.getName().equals(methodName)) {
                    // TODO
                    description = method.getAnnotation(CheckOrderBy.class).description();
                    // 跳出
                    break;
                }
            }
        // 捕捉异常
        } catch (Exception e) {
            logger.info(e.toString());
        }
        // TODO
        judgeOrderBy(request, description);
    }

    /**
     * TODO
    */
    public static void judgeOrderBy(HttpServletRequest request, String description){
        // 字符串赋值
        String orderBy = request.getParameter("orderBy");
        // 判断
        if(!StringUtils.isEmpty(orderBy)){
            // 切分
            String[] orderBys = orderBy.split(" ");
            // 判断
            if(orderBys.length!=2 || !( "asc".equals(orderBys[1].toLowerCase()) || "desc".equals(orderBys[1].toLowerCase())) ){
                // 抛出异常
                throw new CacheServerException(CacheServerErrorCode.CHECKORDERBYAOP_ORDERBY_ILLEGAL);
            }
            // 判断
            if(orderBy.indexOf("insert ")>0  || orderBy.indexOf("use ")>0 || orderBy.indexOf("alter ")>0 || orderBy.indexOf("create ")>0 || orderBy.indexOf("drop ")>0 || orderBy.indexOf("select ")>0 || orderBy.indexOf("truncate ")>0 || orderBy.indexOf("delete ")>0 || orderBy.indexOf("update ")>0){
                // 抛出异常
                throw new CacheServerException(CacheServerErrorCode.CHECKORDERBYAOP_ORDERBY_ILLEGAL);
            }
            logger.info("CheckOrderByAop:验证:orderBy:"+orderBy+":通过:description:"+description);
        }
    }

}
